import {IGenerate} from './igenerate';
import {GenerateOption} from './generateOption';
import {EasyuiGenerate, use} from './generates';
import { COMPTYPE } from './utils';
import { DefaultGenerate } from './defaultGenerate';

/**
 * 生成ko组件，并注册到ko组件对象中
 */
export class GenerateFactory implements IGenerate {
    public option: GenerateOption;
    public generates: any;
    constructor(option:GenerateOption){
        this.option = option;
        this.generates ={
            [COMPTYPE.easyui]: new EasyuiGenerate(this.option),
            [COMPTYPE.norm]: new DefaultGenerate(this.option)
        }
        use(this.option.ko); //引用ko-messager组件
    }

    generate(componentName: string, params: any, viewModel: any):any {
        let generateHandler = this.generates[viewModel.type || COMPTYPE.norm];
        if(generateHandler){
            return generateHandler.generate(componentName, params, viewModel);
        }
        return viewModel;
    }
}